﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace NivdalDashboard
{
    public partial class Form1 : Form
    {

        private int NumTransferMarketErrors = 0;
        private bool TransferMarketProblem = false;
        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.RunWorkerAsync();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                try
                {
                    NextRefreshLabel.Text = "Refreshing data ... ";
                    RefreshData();
                    CheckTransferMarket();
                    SleepForTime(60000);
                }
                catch
                {
                    int NumErrors = int.Parse(NumErrorsLabel.Text);
                    NumErrors++;
                    NumErrorsLabel.Text = NumErrors.ToString();
                    SleepForTime(60000);
                }                
            }            
        }

        void SleepForTime(int MillisecondsToSleep)
        {
            int UpdateTime = 1000;

            while (MillisecondsToSleep >= 1000)
            {
                NextRefreshLabel.Text = "Next refresh in " + (MillisecondsToSleep / 1000).ToString() + " more seconds";
                Thread.Sleep(UpdateTime);
                MillisecondsToSleep -= UpdateTime;
            }
            NextRefreshLabel.Text = "";
        }

        private void RefreshData()
        {
            List<GetNivdalData.ParamValue> InParams = new List<GetNivdalData.ParamValue>();
            List<GetNivdalData.ParamValue> OutParams = new List<GetNivdalData.ParamValue>();

            // input parameters - none for now

            // output parameters: 
            OutParams.Add(new GetNivdalData.ParamValue { Name = "OnlineUsers", Type = SqlDbType.Decimal, Size = 18 });

            if (GetNivdalData.execSP(InParams, OutParams, "Nivdal_DashboardData"))
            {
                // num users
                decimal NumUsers = (decimal)OutParams[0].Value;
                UpdateUsersOnline(NumUsers);
            }
            else
            {
                // connection error:
                int NumErrors = int.Parse(NumErrorsLabel.Text);
                NumErrors++;
                NumErrorsLabel.Text = NumErrors.ToString();
            }

            // transfer market - is it open

        }

        private void UpdateUsersOnline(decimal OnlineUsers)
        {
            OnlineUsersLabel.Text = OnlineUsers.ToString() + " users are currently online";

            double pointsCount = (double)OnlineUsersChart.Series[0].Points.Count;
            OnlineUsersChart.Series[0].Points.AddXY(pointsCount + 1, OnlineUsers);
            OnlineUsersChart.Series[0].Points[(int)pointsCount].AxisLabel = DateTime.Now.ToShortTimeString();
            if (pointsCount > 5)
            {
                OnlineUsersChart.Series[0].Points.Remove(OnlineUsersChart.Series[0].Points[0]);
                try
                {
                    foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint p in OnlineUsersChart.Series[0].Points)
                    {
                        p.XValue--;
                    }
                }
                catch {  }
            }
        }

        private void CheckTransferMarket()
        { 
            List<GetNivdalData.ParamValue> InParams = new List<GetNivdalData.ParamValue>();
            List<GetNivdalData.ParamValue> OutParams = new List<GetNivdalData.ParamValue>();

            if (!GetNivdalData.execSP(InParams, OutParams, "Nivdal_DashboardCheckTransferMarket"))
            {
                TransferMarketStatusLabel.Text = "Transfer market looks stuck. ";
                ResetTransferMarketButton.Visible = true;
                if (!TransferMarketProblem) NumTransferMarketErrors++;
                else TransferMarketProblem = true;
            }
            else
            {
                TransferMarketStatusLabel.Text = "Transfer market looks fine. ";
                ResetTransferMarketButton.Visible = false;
                TransferMarketProblem = false;
            }

            TransferMarketStatusLabel.Text += "Total number of errors is ";
            TransferMarketStatusLabel.Text += NumTransferMarketErrors.ToString();
        }

        private void ResetTransferMarketButton_Click(object sender, EventArgs e)
        {
            TransferMarketStatusLabel.Text = "Recompiling the SP ...";
            GetNivdalData.RecompileTransferMarketSP();
            CheckTransferMarket();
        }
    }
}
